/*
 * Copyright (c) 2018-2999 湖南洛云网络科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.live.multishop.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.LiveInterfaceType;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.i18n.I18nMessage;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.live.comment.model.LiveUser;
import com.yami.shop.live.comment.param.LiveUsableNumParam;
import com.yami.shop.live.comment.param.ReturnLiveParam;
import com.yami.shop.live.comment.service.LiveLogService;
import com.yami.shop.live.comment.service.LiveUserService;
import com.yami.shop.security.multishop.util.SecurityUtils;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


/**
 *
 *
 * @author lhd
 * @date 2020-11-20 17:49:56
 */
@RestController
@AllArgsConstructor
@RequestMapping("/multishop/live/liveUser" )
public class LiveUserController {

    private final LiveUserService liveUserService;
    private final LiveLogService liveLogService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param liveUser
     * @return 分页数据
     */
    @GetMapping("/page" )
    public ResponseEntity<IPage<LiveUser>> getLiveUserPage(PageParam<LiveUser> page, LiveUser liveUser) {
        PageParam<LiveUser> liveUserPage = liveUserService.page(page, new LambdaQueryWrapper<LiveUser>()
                .eq(LiveUser::getShopId, SecurityUtils.getShopUser().getShopId())
                .like(StrUtil.isNotBlank(liveUser.getAnchorWechat()),LiveUser::getAnchorWechat,liveUser.getAnchorWechat())
                .like(StrUtil.isNotBlank(liveUser.getNickName()),LiveUser::getNickName,liveUser.getNickName())
                .like(StrUtil.isNotBlank(liveUser.getRoles()),LiveUser::getRoles,liveUser.getRoles())
                .orderByDesc(LiveUser::getUpdateTime)
        );
        for (LiveUser record : liveUserPage.getRecords()) {
            int[] roles = StrUtil.splitToInt(record.getRoles(), StrUtil.COMMA);
            StringBuilder roleStr = new StringBuilder();
            Map<Integer,String> roleMap = new HashMap<>(16);
            roleMap.put(0, I18nMessage.getMessage("yami.live.user.super.admin"));
            roleMap.put(1, I18nMessage.getMessage("yami.live.user.admin"));
            roleMap.put(2, I18nMessage.getMessage("yami.live.user.anchor"));
            roleMap.put(3, I18nMessage.getMessage("yami.live.user.operators"));
            for (int role : roles) {
                roleStr.append(roleMap.get(role));
            }
            roleStr.deleteCharAt(roleStr.length() - 1);
            record.setRoles(roleStr.toString());
        }
        return ResponseEntity.ok(liveUserPage);
    }


    /**
     * 通过id查询
     * @param liveUserId id
     * @return 单个数据
     */
    @GetMapping("/info/{liveUserId}" )
    public ResponseEntity<LiveUser> getById(@PathVariable("liveUserId") Long liveUserId) {
        return ResponseEntity.ok(liveUserService.getById(liveUserId));
    }

    /**
     * 新增
     * @param liveUser
     * @return 是否新增成功
     */
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('live:liveUser:save')")
    public ResponseEntity<ReturnLiveParam> save(@RequestBody @Valid LiveUser liveUser) throws WxErrorException {
        liveUser.setShopId(SecurityUtils.getShopUser().getShopId());
        return ResponseEntity.ok(liveUserService.saveInfo(liveUser));
    }

    /**
     * 修改
     * @param liveUser
     * @return 是否修改成功
     */
    @SysLog("修改" )
    @PutMapping
    public ResponseEntity<Boolean> updateById(@RequestBody @Valid LiveUser liveUser) {
        return ResponseEntity.ok(liveUserService.updateById(liveUser));
    }

    /**
     * 通过id删除
     * @param liveUserId id
     * @return 是否删除成功
     */
    @SysLog("删除" )
    @DeleteMapping("/{liveUserId}" )
    @PreAuthorize("@pms.hasPermission('live:liveUser:delete')")
    public ResponseEntity<Boolean> removeById(@PathVariable Long liveUserId) throws WxErrorException{
        return ResponseEntity.ok(liveUserService.removeAndWeChatById(liveUserId));
    }

    /**
     * 查询商家今日剩余可用添加直播商品、可删除商品库商品次数
     */
    @GetMapping("/getAddProdNum" )
    public ResponseEntity<LiveUsableNumParam> getAddRoomNum() {
        LiveUsableNumParam liveNum = liveLogService.getLiveNum(SecurityUtils.getShopUser().getShopId(), LiveInterfaceType.ADD_LIVE_ROLE);
        LiveUsableNumParam deleteLiveNum = liveLogService.getLiveNum(SecurityUtils.getShopUser().getShopId(), LiveInterfaceType.REMOVE_LIVE_ROLE);
        liveNum.setDeleteShopNum(deleteLiveNum.getShopNum());
        liveNum.setDeleteTotalNum(deleteLiveNum.getTotalNum());
        return ResponseEntity.ok(liveNum);
    }

}
